package meterMonitor.biz;

import meterMonitor.mapper.MonitorDetailMapper;
import meterMonitor.model.MonitorDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class MonitorDetailBiz {
    @Autowired
    MonitorDetailMapper detailMapper;

    public MonitorDetail getCurrentByMeterCode(Long meterCode) {
        return detailMapper.getCurrentByMeterCode(meterCode);
    }

    public double queryMonthTotal(long meterCode) {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.DAY_OF_MONTH, 1);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        Double total = detailMapper.queryCurrentMonth(new Timestamp(instance.getTimeInMillis()), meterCode);
        return null == total ? 0d : total;
    }

    public List<MonitorDetail> queryData(String fromTime, String toTime, String codes) {
        List<MonitorDetail> monitorDetails = detailMapper.queryData(fromTime, toTime, codes);
        DecimalFormat df = new DecimalFormat("#0.00");

        for (MonitorDetail detail : monitorDetails) {

            detail.setStandard(Double.valueOf(df.format(detail.getStandard() / 24)));
            if (detail.getStandard() < detail.getIntervalQuantity()) {
                detail.setIsOver("是");
            } else {
                detail.setIsOver("否");
            }
        }
        return monitorDetails;
    }


}
